//
//  DEHomeMainCell.m
//  DE
//
//  Created by PEIYANG on 16/9/24.
//  Copyright © 2016年 YP. All rights reserved.
//
#define imageMargin 20
#define imageCount 3
#define imageW (YPScreenW - ((imageCount - 1) * imageMargin * 2 + 2 * imageMargin)) / imageCount
#import "DEHomeMainCell.h"

@implementation DEHomeMainCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImage *image = [UIImage imageNamed:@"whiteBg"];
        image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
        self.backgroundView = [[UIImageView alloc]initWithImage:image];
        [self setupSubviews];
    }
    return self;
}

#warning ------ 内存消耗很大
//- (void)layoutSubviews{
//    [super layoutSubviews];
//}

- (void)setupSubviews{
    //左边标题
    UILabel *leftLabel = [[UILabel alloc]init];
    leftLabel.text = @"最新上";
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.textColor = [UIColor blackColor];
    CGSize leftLabelSize = [leftLabel.text sizeWithFont:[UIFont systemFontOfSize:16]];
    leftLabel.frame = (CGRect){{imageMargin, imageMargin}, leftLabelSize};
    [self.contentView addSubview:leftLabel];
    
    //右边标题
    UIButton *rightBtn = [[UIButton alloc]init];
    
//    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
//    textAttachment.image = [UIImage imageNamed:@"SVWebViewControllerBack"];
//    NSAttributedString *muStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
//    NSString *str = @"查看全部13条";
    [rightBtn setTitle:@"查看全部13条" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        [rightBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    rightBtn.frame = CGRectMake(0, imageMargin, YPScreenW - imageMargin, leftLabel.yp_height);
    [rightBtn setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:rightBtn];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //cell图片
    CGFloat btnY = leftLabel.yp_bottom + imageMargin;
    CGFloat imageH = imageW * 4 / 3;
    CGFloat imageX;
    for (int index = 0; index < imageCount; index++) {
        NSString *imageName = [NSString stringWithFormat:@"home%d",index];
        UIImage *image = [UIImage imageNamed:imageName];
        
        UIButton *btn = [[UIButton alloc]init];
        
        btn.tag = index;
        [btn setImage:image forState:UIControlStateNormal];
        imageX = imageMargin + (imageW + 2 * imageMargin) * index;
        
        btn.frame = CGRectMake(imageX, btnY, imageW, imageH);
        [self.contentView addSubview:btn];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //设置图片下面的文字
        UILabel *bottomLabel = [[UILabel alloc]init];
        bottomLabel.text = @"据说这是一个非常牛逼的APP";
        CGSize bottomLabelSize = [bottomLabel.text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(imageW, MAXFLOAT)];
        bottomLabel.font = [UIFont systemFontOfSize:12];
        bottomLabel.textColor = [UIColor lightGrayColor];
        bottomLabel.numberOfLines = 0;

        bottomLabel.frame = (CGRect){{imageX, btn.yp_bottom + imageMargin / 2}, bottomLabelSize};
        [self.contentView addSubview:bottomLabel];
        
        _cellH = bottomLabel.yp_bottom + imageMargin;

        
    }
    
}
- (void)rightBtnClick{
    NSLog(@"右边按钮点击");
}
- (void)btnClick:(UIButton *)button{
    NSLog(@"点击了第%zd张图片",button.tag);
}
@end
